let { list } = require("./mock");
const bodyParser = require("body-parser");
module.exports = (app) => {
  app.use(bodyParser.json());

  app.get("/api/list", (req, res) => {
    res.send({
      code: 200,
      data: list,
    });
  });

  app.delete("/api/list/:id", (req, res) => {
    const { id } = req.params;
    list = list.filter((v) => v.id !== id);
    res.send({
      code: 200,
      message: "删除成功",
    });
  });

  app.post("/api/list", (req, res) => {
    console.log(req.body)
    const { title, tel, name, sex, address } = req.body;

    list.unshift({
      title,
      tel,
      name,
      sex,
      address,
      id: Math.random().toString().slice(2),
    });

    res.send({
      code: 200,
      message: "添加成功",
    });
  });

	app.post("/api/list/:id", (req, res) => {
		const { id } = req.params
		const item = list.find(v => v.id === id)

		Object.assign(item, req.body)


    res.send({
      code: 200,
      message: "修改成功",
    });
  });
};
